package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Override
    public List<Category> list() {
        List<Category> list = categoryMapper.listByType();
        return list;
    }

    @Override
    public void deleteCategory(int id) {
        categoryMapper.delete(id);
    }

    @Override
    public Category addCategory(Category category) {
        category.setStatus(0);
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());
        categoryMapper.insertCategory(category);
        return category;
    }

    @Override
    public Category update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());

        categoryMapper.updateCategory(category);

        return category;
    }

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void startOrStop(String status,Long id) {
        Category category = new Category();
        category.setStatus(Integer.parseInt(status));
        category.setId(id);
        categoryMapper.startOrStop(category);
    }

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {

        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category>page = categoryMapper.pageQuery(categoryPageQueryDTO);

        List<Category> result = page.getResult();
        long total = page.getTotal();

        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);

        return pageResult;
    }



}
